package com.tiantianyuegou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tiantianyuegou.activity.BaseActivity;
import com.tiantianyuegou.R;
import com.tiantianyuegou.constant.Constant;
import com.tiantianyuegou.util.Tools;
import com.tiantianyuegou.view.pass.GridPasswordView;

/**
 * 设置支付密码界面
 * Created by ivany on 2016/1/14.
 */
public class SettingBalanceActivity extends BaseActivity implements View.OnClickListener, GridPasswordView.OnPasswordChangedListener {

    private Button mBtn_next;
    private Button mBtn_1, mBtn_2, mBtn_3, mBtn_4, mBtn_5, mBtn_6, mBtn_7, mBtn_8, mBtn_9, mBtn_0;
    private ImageButton mBtn_delete;
    private TextView mTxt_tip;
    //密码输入框
    GridPasswordView mPasswordView;
    private LinearLayout mLinear_keyboard;

    private String mPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_balance);
        Tools.changeSystemBar(this);
        setTitle("设置支付密码");
        initViews();
    }

    @Override
    public void findViewByIds() {
        mBtn_next = (Button) findViewById(R.id.setting_balance_next);
        mBtn_0= (Button) findViewById(R.id.btn_key_0);
        mBtn_1= (Button) findViewById(R.id.btn_key_1);
        mBtn_2= (Button) findViewById(R.id.btn_key_2);
        mBtn_3= (Button) findViewById(R.id.btn_key_3);
        mBtn_4= (Button) findViewById(R.id.btn_key_4);
        mBtn_5= (Button) findViewById(R.id.btn_key_5);
        mBtn_6= (Button) findViewById(R.id.btn_key_6);
        mBtn_7= (Button) findViewById(R.id.btn_key_7);
        mBtn_8= (Button) findViewById(R.id.btn_key_8);
        mBtn_9= (Button) findViewById(R.id.btn_key_9);
        mBtn_delete= (ImageButton) findViewById(R.id.btn_key_del);
        mPasswordView= (GridPasswordView) findViewById(R.id.setting_balande_password);
        mLinear_keyboard= (LinearLayout) findViewById(R.id.setting_balance_keyboard);
        mTxt_tip= (TextView) findViewById(R.id.setting_balance_tip);
        mTxt_tip.setText("请输入6位数字支付密码");
    }

    @Override
    public void initListener() {
        mBtn_next.setOnClickListener(this);
        mBtn_0.setOnClickListener(this);
        mBtn_1.setOnClickListener(this);
        mBtn_2.setOnClickListener(this);
        mBtn_3.setOnClickListener(this);
        mBtn_4.setOnClickListener(this);
        mBtn_5.setOnClickListener(this);
        mBtn_6.setOnClickListener(this);
        mBtn_7.setOnClickListener(this);
        mBtn_8.setOnClickListener(this);
        mBtn_9.setOnClickListener(this);
        mBtn_delete.setOnClickListener(this);
        mPasswordView.setOnPasswordChangedListener(this);
        mPasswordView.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setting_balande_password:
                mLinear_keyboard.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_key_0:
                mPasswordView.appentPassWord("0");
                break;
            case R.id.btn_key_1:
                mPasswordView.appentPassWord("1");
                break;
            case R.id.btn_key_2:
                mPasswordView.appentPassWord("2");
                break;
            case R.id.btn_key_3:
                mPasswordView.appentPassWord("3");
                break;
            case R.id.btn_key_4:
                mPasswordView.appentPassWord("4");
                break;
            case R.id.btn_key_5:
                mPasswordView.appentPassWord("5");
                break;
            case R.id.btn_key_6:
                mPasswordView.appentPassWord("6");
                break;
            case R.id.btn_key_7:
                mPasswordView.appentPassWord("7");
                break;
            case R.id.btn_key_8:
                mPasswordView.appentPassWord("8");
                break;
            case R.id.btn_key_9:
                mPasswordView.appentPassWord("9");
                break;
            case R.id.btn_key_del:
                mPasswordView.deletePassWord();
                break;
            case R.id.setting_balance_next:
                Intent intent=new Intent(SettingBalanceActivity.this,ConfirmBalanceActivity.class);
                intent.putExtra("password",mPassword);
                startActivityForResult(intent, Constant.CONFIRM_BALANCE_REQUEST);
                break;
        }
    }

    @Override
    public void onChanged(String psw) {
    }

    @Override
    public void onMaxLength(String psw) {
        mBtn_next.setAlpha(1.0f);
        mBtn_next.setEnabled(true);
        mPassword=psw;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==Constant.CONFIRM_BALANCE_REQUEST){
            if(resultCode==Constant.CONFIRM_BALANCE_RESULT){
                Intent intent=new Intent();
                setResult(Constant.SETTING_BALANCE_RESULT,intent);
                finish();
            }
        }
    }
}
